//
//  YDPictureView.swift
//  weibo
//
//  Created by 孙亚东 on 16/6/28.
//  Copyright © 2016年 Sunyadong. All rights reserved.
//

import UIKit

let margin: CGFloat = 10

let itemWidth: CGFloat = (screenWidth - 40)/3
let itemHeight = itemWidth



class YDPictureView: UICollectionView  {

    
    //获取图片的张数和URL
    var picturesViewModels: YDPhotoViewModel? {
    
        didSet {
            
           let count = (picturesViewModels?.photo?.count)!
            
            if (picturesViewModels?.photo?.count)! == 0 {
            
                self.snp_updateConstraints { (make) -> Void in
                    
                    
                    make.height.equalTo(0)
                    
                }
            }else{

            let row = (count - 1)/3 + 1
   
            let height = CGFloat(row) * itemHeight  + (CGFloat(row) - 1) * margin

            // 更新约束
            self.snp_updateConstraints { (make) -> Void in
                
                
                make.height.equalTo(height)
                
                
            }
 
                
            }
            reloadData()
    
        
        }
        

    }

    
    
    
    //自定义一个layout
    
    let layoutFlow = UICollectionViewFlowLayout()

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        
        super.init(frame: frame, collectionViewLayout: layoutFlow)
        
        backgroundColor = UIColor.clearColor()
        
         layoutFlow.itemSize = CGSizeMake(itemWidth, itemHeight)
        
        layoutFlow.minimumInteritemSpacing = margin
        layoutFlow.minimumLineSpacing = margin
        
        
        //注册cell
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        
    }
    
    
    func setupUI () {
    
    
        dataSource = self
        
        
        registerClass(YDPictureViewCell.self, forCellWithReuseIdentifier: "coll")
        
    
    
    }
    


}


//实现数据源方法

extension YDPictureView: UICollectionViewDataSource {

    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return picturesViewModels?.photo?.count ?? 0
    
        
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("coll", forIndexPath: indexPath) as! YDPictureViewCell
        
        if let viewModel = picturesViewModels {
        
           cell.photoModel = viewModel.photo![indexPath.row]
        
        }
      
        return cell
        
        
    }
    






}


//自定义cell
class YDPictureViewCell: UICollectionViewCell {
    
    
    
    //进行赋值
    var photoModel: YDPictureModels? {
    
    
        didSet {
            
            guard let str = photoModel?.thumbnail_pic else {
            
                return
            
            }
    
            imageView.sd_setImageWithURL(NSURL(string: str))
            
 
        }
        
    
    
    }
    
    


    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    func setupUI () {
    
       contentView.addSubview(imageView)
        
        
        //设置约束
        imageView.snp_makeConstraints { (make) -> Void in
            
            make.top.right.left.bottom.equalTo(contentView)
            
            
        }
   
    
    }

    //懒加载控件
    lazy var imageView: UIImageView = {
    
        let imageView = UIImageView()
        
        imageView.contentMode = .ScaleAspectFill
        
        imageView.clipsToBounds = true
        
        return imageView
    
    
    
    
    }()
    
    


}




